package com.guigu.pd.auth.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.guigu.pd.auth.entity.TruckDriverLicenseEntity;
import com.guigu.pd.auth.feign.AuthUserFeign;
import com.guigu.pd.base.entity.TruckDriverEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.guigu.pd.auth.entity.AuthUserEntity;
import com.guigu.pd.auth.service.AuthUserService;
import com.guigu.common.utils.PageUtils;
import com.guigu.common.utils.R;



/**
 * 
 *
 * @author deyran
 * @email deyran@gmail.com
 * @date 2022-07-19 19:05:57
 */
@RestController
@RequestMapping("auth/authuser")
public class AuthUserController {
    @Autowired
    private AuthUserService authUserService;

    @Autowired
    private AuthUserFeign authUserFeign;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("auth:authuser:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = authUserService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("auth:authuser:info")
    public R info(@PathVariable("id") Long id){
		AuthUserEntity authUser = authUserService.getById(id);

        return R.ok().put("authUser", authUser);
    }

    @GetMapping("/getuser/{id}")
    //@RequiresPermissions("auth:authuser:info")
    public String getUser(@PathVariable("id")String id){
        AuthUserEntity authUser = authUserService.getById(id);

        return authUser.getName();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("auth:authuser:save")
    public R save(@RequestBody AuthUserEntity authUser){
        //判断用户是否是司机
        if(authUser.getStationId()==2){
            TruckDriverLicenseEntity driverLicense = authUser.getTruckDriverLicenseEntity();
            authUserFeign.savedriverlicense(driverLicense);
            TruckDriverEntity driver = new TruckDriverEntity(authUser.getId(), authUser.getFleetid());
            authUserFeign.savedriver(driver);
        }else {
            authUserService.save(authUser);
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("auth:authuser:update")
    public R update(@RequestBody AuthUserEntity authUser){
		authUserService.updateById(authUser);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("auth:authuser:delete")
    public R delete(@RequestBody Long[] ids){
		authUserService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/getnamebyid/{id}")
    public String getnamebyid(@PathVariable("id") String id){
        AuthUserEntity authUser = authUserService.getById(id);

        String str = authUser.getName()+","+authUser.getOrgId()+","+authUser.getMobile();


        return str;
    }

    /**
     * 根据网点id获取该网点下的快递员编号
     * @param id
     * @return
     */
    @GetMapping("/getuserkdy/{id}")
    public String getuserkdy(@PathVariable("id")String id){

        LambdaQueryWrapper<AuthUserEntity> lambdaQueryWrapper = new LambdaQueryWrapper<AuthUserEntity>();
        lambdaQueryWrapper.eq(AuthUserEntity::getOrgId,id);

        List<AuthUserEntity> list = authUserService.list(lambdaQueryWrapper);

       String str  = "";

        for (int i = 0;i<list.size();i++) {

            if(i<list.size()-1){

                str = str + list.get(i).getId()+",";
            }else{
                str = str + list.get(i).getId();
            }




        }

        return str;


    }

    @GetMapping("/getcourierr")
    public Map<String, String> getallCourier(){
        QueryWrapper<AuthUserEntity> queryWrapper = new  QueryWrapper<AuthUserEntity>();
        queryWrapper.eq("station_id",3);
        List<AuthUserEntity> list = authUserService.list(queryWrapper);
        Map<String, String> cmap = new HashMap<String, String>();
        for (AuthUserEntity authUser : list) {
            cmap.put(authUser.getId(),authUser.getName());
        }
        return cmap;
    }

    @GetMapping("/getuserbyid/{id}")
    public String getuserbyId(@PathVariable("id")String id){
        AuthUserEntity authUser = authUserService.getById(id);
        String user = authUser.getName()+","+authUser.getMobile();

        return user;
    }

    @GetMapping("/getuserinfobyid/{id}")
    public String getuserinfobyId(@PathVariable("id")String id){
        AuthUserEntity authUser = authUserService.getById(id);
        if(authUser!=null) {
            return authUser.getName();
        }else {
            return null;
        }
    }

    @GetMapping("/getdriverinfo")
    public List<AuthUserEntity> getdriverinfos(){
        QueryWrapper<AuthUserEntity> queryWrapper = new QueryWrapper<AuthUserEntity>();
        queryWrapper.eq("station_id",2);
        List<AuthUserEntity> list = authUserService.list(queryWrapper);
        return list;
    }

    @GetMapping("/getidbyname/{name}")
    public String getidbyName(@PathVariable("name") String name){
        QueryWrapper<AuthUserEntity> queryWrapper = new QueryWrapper<AuthUserEntity>();
        queryWrapper.eq("name",name);
        AuthUserEntity userinfo = authUserService.getOne(queryWrapper);
        return userinfo.getId();
    }

    @GetMapping("/getdriversum")
    public Integer getdriversum(){
        QueryWrapper<AuthUserEntity> queryWrapper = new QueryWrapper<AuthUserEntity>();
        queryWrapper.eq("station_id", 2);
        List<AuthUserEntity> list = authUserService.list(queryWrapper);
        return list.size();
    }


}
